// pages/my-info/my-info.js
const apiurl = require('../../config').apiurl;
import {
  Pulibc
} from '../../models/public.js'
const pulibc = new Pulibc();
Page({
  uploadCode() {
    wx.uploadFile({
      url
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    name: '',
    showUserInfo: false,
    index: '',
    member_name: '', // input框输入的字段
    wx_avatar: '',
    member_qq: '',
    member_truename: '',
    member_birthday: '',
    occupation: '',
    member_name_edit: '',  //编辑后的字段
    wx_avatar_edit: '',
    member_qq_edit: '',
    member_truename_edit: '',
    member_birthday_edit: '',
    occupation_edit: '',
    genderList:[
      {index:'1',name:'男'},
      {index:'2',name:'女'}
    ],
    sex_index:''
  },

  // 编辑会员名称
  getEditMemberItem(memberItem) {
    let url = apiurl + '/memberaccount/edit'
    let item = JSON.stringify(memberItem);
    let params = JSON.parse(item);
    params.key = wx.getStorageSync('key');
   let editKey = Object.keys(JSON.parse(item))[0]+'_edit';
   let editValue = Object.values(JSON.parse(item))[0];
    console.log(editKey,editValue)
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method: 'POST',
      // data:{
      //   key,
      //   memberItem,
      //   // member_truename:'测试111',

      //   // wx_avatar:'9999',
      //   // member_qq:'123456',
      //   // member_sex:'1',
      //   // member_birthday:'2019-01-01',
      //   // occupation:'其它'
      // },
      data: params,
      success: data => {
        console.log(data)
        if (data.data.code == 200) {
          pulibc.showToast('编辑成功', 'none')
          this.setData({
            [editKey]:editValue,
            showUserInfo: false
          })
        }
      }

    })
  },
  // 修改用户名
  updataName(e) {
    let name = e.currentTarget.dataset.name
    let index = e.currentTarget.dataset.index
    this.setData({
      name,
      showUserInfo: true,
      index: index
    })
  },
  // 隐藏遮罩层
  hideUserInfo() {
    this.setData({
      showUserInfo: false
    })
  },

  // 获取用户输入
  getMemberName(e) {
    // console.log()
    let member_name = e.detail.value
    if (member_name == '') {
      pulibc.showToast('输入不能为空11', 'none')
      return;
    }
    this.setData({
      member_name
    })
  },
  getWeixin(e) {
    // console.log()
    let wx_avatar = e.detail.value
    this.setData({
      wx_avatar
    })
  },
  getMemberQQ(e) {
    // console.log()
    let member_qq = e.detail.value
    this.setData({
      member_qq
    })
  },
  getTrueName(e) {
    // console.log()
    let member_truename = e.detail.value
    this.setData({
      member_truename
    })
  },
  getOccupation(e) {
    // console.log()
    let occupation = e.detail.value
    this.setData({
      occupation
    })
  },
  // 性别选择器
  bindGenderChange(e){
    let member_sex = parseInt(e.detail.value);
    console.log("member_sex", member_sex)
    this.data.genderList.map((item,index)=>{
      if (parseInt(parseInt(item.index) - 1) == member_sex){
        this.setData({
          member_sex_edit: member_sex+1,
          member_sex: item.name,
        })
      }
    })
    member_sex = member_sex + 1;
    this.getEditMemberItem({
      member_sex
    })
    if (this.data.member_sex_edit=="1"){
      this.setData({
        member_sex:'男'
      })
    }else {
      this.setData({
        member_sex:'女'
      })
    }
  },
  // 生日-日期选择器
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let member_birthday=e.detail.value;
    this.setData({
      member_birthday: e.detail.value
    })
    this.getEditMemberItem({
      member_birthday
    })
  },
  // 提交
  editorSubmit(e) {
    // 1名称 2微信 3QQ 4真实名 5性别 6生日 7职业
    let index = e.currentTarget.dataset.index
    let member_name = this.data.member_name;
    let wx_avatar = this.data.wx_avatar;
    let member_qq = this.data.member_qq;
    let member_truename = this.data.member_truename;
    let occupation = this.data.occupation;

    if (index == 1) {
      if (member_name == '') {
        pulibc.showToast('输入不能为空', 'none')
        return;
      }
      this.getEditMemberItem({
        member_name
      })
    } else if (index == 2) {
      if (wx_avatar == '') {
        pulibc.showToast('输入不能为空', 'none')
        return;
      }
      this.getEditMemberItem({
        wx_avatar
      })
    } else if (index == 3) {
      if (member_qq == '') {
        pulibc.showToast('输入不能为空', 'none')
        return;
      }
      this.getEditMemberItem({
        member_qq
      })
    } else if (index == 4) {
      if (member_truename == '') {
        pulibc.showToast('输入不能为空', 'none')
        return;
      }
      this.getEditMemberItem({
        member_truename
      })
    }  else if (index == 7) {
      if (occupation== '') {
        pulibc.showToast('输入不能为空', 'none')
        return;
      }
      this.getEditMemberItem({
        occupation
      })
    }



  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {


  },
  getDataInfo() {
    let key = wx.getStorageSync('key')
    // https://shop.paisc.cn/public/wxapi/memberaccount/info
    let url = apiurl + '/memberaccount/info'
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method: "POST",
      data: {
        key
      },
      success: data => {
        console.log(data)
        let code = data.data.code
        if (code == 200) {
          let info = data.data.datas.member_info
          this.setData({
            info: info,
            // input框的字段
            member_name: info.member_name,
            wx_avatar: info.wx_avatar ? info.wx_avatar : '',
            member_qq: info.member_qq ? info.member_qq : '',
            member_truename: info.member_truename ? info.member_truename : '',
            sex_index:info.member_sex==1?'1':'2',  //控制性别的显示
            member_birthday: info.member_birthday ? info.member_birthday : '',
            occupation: info.occupation ? info.occupation : '',
            // 编辑的字段
            member_name_edit: info.member_name,
            wx_avatar_edit: info.wx_avatar ? info.wx_avatar : '',
            member_qq_edit: info.member_qq ? info.member_qq : '',
            member_sex: info.member_sex == 1 ? '男' : '女',  
            member_truename_edit: info.member_truename ? info.member_truename : '',
            member_birthday_edit: info.member_birthday ? info.member_birthday : '',
            occupation_edit: info.occupation ? info.occupation : '',
          })
        } else if (data.data.datas.error == '请登录') {
          wx.navigateTo({
            url: "../register/register"
          })
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    } else {
      this.getDataInfo()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})